
package com.thunbu.lib_common.utils;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.thunbu.lib_common.base.BaseApplication;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import static com.thunbu.lib_common.config.CommConstants.TOKEN;

/**
 * @author Yang.Wang9
 */
public class TokenInterceptor implements Interceptor {

    private static final String TAG = "TokenInterceptor";

    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        Request original = chain.request();
        HttpUrl httpUrl = original.url().newBuilder().build();

        Request.Builder newBuilder = original.newBuilder();
        newBuilder.header("Content-Type", "application/json");
//        if (BaseApplication.myApplication.getMacAddress() != null) {
//            newBuilder.addHeader("screenEquipId", BaseApplication.myApplication.getMacAddress());
//        }
        String token = MFSPHelper.getString(TOKEN, null);
        if(!TextUtils.isEmpty(token)){
            //其实塞的是userid
            newBuilder.addHeader("userid",token);
        }
        Request request = newBuilder.method(original.method(), original.body())
                .url(httpUrl)
                .build();
        Response response = chain.proceed(request);
        return response;
    }

}
